package com.mj.app.health.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import com.mj.app.health.MyApplication;
import com.mj.app.health.R;

/**
 * ========================================
 * *
 * title: CommonNoticeDialog
 * description: 一般提示dialog
 * *
 * Created by David Hogan on 2017/12/21.
 * ========================================
 */

public class CommonNoticeDialog extends Dialog {

    private Context context;
    private ICommonDialog iCommonDialog;

    private String id;
    private int removePosition = -1;
    private String titleTxt, msgTxt, cancelTxt, sureTxt;
    private TextView title, dia_msg, dia_cancel, dia_sure;

    public void setICommonDialog(ICommonDialog ICommonDialog){
        this.iCommonDialog = ICommonDialog;
    }

    public CommonNoticeDialog(@NonNull Context context) {
        this(context, R.style.DarkFullScreenDialog);
        this.context = context;

        init();
    }

    public CommonNoticeDialog(@NonNull Context context, String id, int removePosition) {
        this(context, R.style.DarkFullScreenDialog);
        this.context = context;
        this.id = id;
        this.removePosition = removePosition;

        init();
    }

    public CommonNoticeDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
        this.context = context;

        init();
    }

    @Override
    public void setCanceledOnTouchOutside(boolean cancel) {
        super.setCanceledOnTouchOutside(cancel);
    }

    private void init(){
        View rootView = LayoutInflater.from(context).inflate(R.layout.dialog_common, null);
        setContentView( rootView);

        title = rootView.findViewById(R.id.dia_title);
        dia_msg = rootView.findViewById(R.id.dia_msg);
        dia_cancel = rootView.findViewById(R.id.dia_cancel);
        dia_sure = rootView.findViewById(R.id.dia_sure);

        dia_msg.setText(getMsgTxt());

        if(getTitleTxt() != null && !"".equals(getTitleTxt())){
            title.setText(getTitleTxt());
        }
        if(getCancelTxt() != null && !"".equals(getCancelTxt())){
            dia_cancel.setText(getCancelTxt());
        }
        if(getSureTxt() != null && !"".equals(getSureTxt())){
            dia_sure.setText(getSureTxt());
        }

        Window window = getWindow();
        window.setLayout((int) (MyApplication.getScreenWidth()*0.6), ViewGroup.LayoutParams.WRAP_CONTENT);
        window.setWindowAnimations(R.style.DialogBottomAnimation);
        setCanceledOnTouchOutside(true);
        setCancelable(true);

        dia_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(iCommonDialog !=null)
                    iCommonDialog.onCancelPressed();

                dismiss();
            }
        });

        dia_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(iCommonDialog !=null)
                    iCommonDialog.onSurePressed();

                dismiss();
            }
        });
    }

    public String getTitleTxt() {
        return titleTxt;
    }

    public void setTitleTxt(String titleTxt) {
        this.titleTxt = titleTxt;
        title.setText(titleTxt);
    }

    public String getMsgTxt() {
        return msgTxt;
    }

    public void setMsgTxt(String msgTxt) {
        this.msgTxt = msgTxt;
        dia_msg.setText(msgTxt);
    }

    public String getCancelTxt() {
        return cancelTxt;
    }

    public void setCancelTxt(String cancelTxt) {
        this.cancelTxt = cancelTxt;
        dia_cancel.setText(cancelTxt);

    }

    public String getSureTxt() {
        return sureTxt;
    }

    public void setSureTxt(String sureTxt) {
        this.sureTxt = sureTxt;
        dia_sure.setText(sureTxt);
    }
}
